Entity ๊ธฐ๋ณธ ์์ฑ(Annotation)
01. Entity๋ฅผ ๋ํ๋ด๋ ์ด๋ ธํ ์ด์
@Entity
์ด๋
ธํ
์ด์
์ ์ด์ฉ!
โถ๏ธ @Id
๋ผ๋ ๊ธฐ๋ณธํค๋ฅผ ๋ํ๋ด๋ ์ด๋
ธํ
์ด์
๋ ๋ฐ๋์ ํ์!
02. @GeneratedValue
/*
* Copyright (c) 2008, 2019 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
// Contributors:
// Linda DeMichiel - 2.1
// Linda DeMichiel - 2.0
package javax.persistence;
import java.lang.annotation.Target;
import java.lang.annotation.Retention;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static javax.persistence.GenerationType.AUTO;
/**
* Provides for the specification of generation strategies for the
* values of primary keys.
*
* <p> The <code>GeneratedValue</code> annotation
* may be applied to a primary key property or field of an entity or
* mapped superclass in conjunction with the {@link Id} annotation.
* The use of the <code>GeneratedValue</code> annotation is only
* required to be supported for simple primary keys. Use of the
* <code>GeneratedValue</code> annotation is not supported for derived
* primary keys.
*
* <pre>
*
* Example 1:
*
* @Id
* @GeneratedValue(strategy=SEQUENCE, generator="CUST_SEQ")
* @Column(name="CUST_ID")
* public Long getId() { return id; }
*
* Example 2:
*
* @Id
* @GeneratedValue(strategy=TABLE, generator="CUST_GEN")
* @Column(name="CUST_ID")
* Long id;
* </pre>
*
* @see Id
* @see TableGenerator
* @see SequenceGenerator
*
* @since 1.0
*/
@Target({METHOD, FIELD})
@Retention(RUNTIME)
public @interface GeneratedValue {
/**
* (Optional) The primary key generation strategy
* that the persistence provider must use to
* generate the annotated entity primary key.
*/
GenerationType strategy() default AUTO;
/**
* (Optional) The name of the primary key generator
* to use as specified in the {@link SequenceGenerator}
* or {@link TableGenerator} annotation.
* <p> Defaults to the id generator supplied by persistence provider.
*/
String generator() default "";
}
/*
* Copyright (c) 2008, 2019 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
// Contributors:
// Linda DeMichiel - 2.1
// Linda DeMichiel - 2.0
package javax.persistence;
/**
* Defines the types of primary key generation strategies.
*
* @see GeneratedValue
*
* @since 1.0
*/
public enum GenerationType {
/**
* Indicates that the persistence provider must assign
* primary keys for the entity using an underlying
* database table to ensure uniqueness.
*/
TABLE,
/**
* Indicates that the persistence provider must assign
* primary keys for the entity using a database sequence.
*/
SEQUENCE,
/**
* Indicates that the persistence provider must assign
* primary keys for the entity using a database identity column.
*/
IDENTITY,
/**
* Indicates that the persistence provider should pick an
* appropriate strategy for the particular database. The
* <code>AUTO</code> generation strategy may expect a database
* resource to exist, or it may attempt to create one. A vendor
* may provide documentation on how to create such resources
* in the event that it does not support schema generation
* or cannot create the schema resource at runtime.
*/
AUTO
}
@GeneratedValue
๋ฅผ ์ถ์ ํ๋ค ๋ณด๋ฉด, GenerationType
์ด๋ผ๋ ์ด๊ฑฐํ์ ํ์ธํด๋ณผ ์ ์๋๋ฐ, ์ด ์ด๊ฑฐํ์ ํ
์ด๋ธ/์ํ์ค/Identity/Auto ์ด 4 ๊ฐ์ง ํ์
์ ์ ๋ต์ ์ง์ํ๋ ๊ฒ์ ํ์ธํด๋ณผ ์ ์๋ค
- Table ์ ๋ต์ ์์ด๋๋ฅผ ๊ด๋ฆฌํ ๋ณ๋์ ํ ์ด๋ธ์ ๋ง๋ค์ด๋๊ณ , ํ ์ด๋ธ์์ ์์ด๋๊ฐ์ ๊ณ์ ์ถ์ถํด์ ์ฌ์ฉํ๋๋ก ์ ๊ณต
- SEQUENCE ์ ๋ต์ ์ค๋ผํด์์ ์์ฃผ ์ฌ์ฉํ๋ ์ ๋ต
-H2 db์์๋ ์ด ์ ๋ต์ ์์ฃผ ์ฌ์ฉ
- Identity ์ ๋ต์ MySQL์์ ์์ฃผ ์ฌ์ฉํ๋ ์ ๋ต -auto_increment๋ฅผ ์ด์ฉํด์ ๊ธฐ๋ณธํค๊ฐ์ ์ฆ๊ฐ์ํด
- ์ผ๋ฐ์ ์ผ๋ก AUTO์ ๋ต์ ๊ธฐ๋ณธ๊ฐ์ฒ๋ผ ์ค์ ํ์ฌ ์ฌ์ฉํ๋๋ฐ, ์ด๋ ๊ฒ ๋ ๊ฒฝ์ฐ ๊ฐ DB์ ๋ง๋ ๊ฐ์ ์๋์ผ๋ก ์ฐ๊ฒฐํด์ฃผ์ด ์ฌ์ฉํ๊ฒ ๋จ[DB์์กด์ฑ์ด ์๋ ์ฅ์ !]
- AUTO ์ ๋ต์ด DB์์กด์ฑ์ด ์๋ค๋ ์ ์์ ์ฅ์ ์ ๊ฐ๊ณ ๋ ์์ง๋ง, ํน์ DB๋ฅผ ์ฐ๊ฒฐํ์ฌ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ๊ฐ ๋ง๊ธฐ ๋๋ฌธ์ ์ ๋ต์ ์ ํด์ฃผ๊ณ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ๊ฐ ๋ง์ด ์๋ค!
03. @Table
/*
* Copyright (c) 2008, 2019 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
// Contributors:
// Linda DeMichiel - 2.1
// Linda DeMichiel - 2.0
package javax.persistence;
import java.lang.annotation.Target;
import java.lang.annotation.Retention;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* Specifies the primary table for the annotated entity. Additional
* tables may be specified using {@link SecondaryTable} or {@link
* SecondaryTables} annotation.
*
* <p> If no <code>Table</code> annotation is specified for an entity
* class, the default values apply.
*
* <pre>
* Example:
*
* @Entity
* @Table(name="CUST", schema="RECORDS")
* public class Customer { ... }
* </pre>
*
* @since 1.0
*/
@Target(TYPE)
@Retention(RUNTIME)
public @interface Table {
/**
* (Optional) The name of the table.
* <p> Defaults to the entity name.
*/
String name() default "";
/** (Optional) The catalog of the table.
* <p> Defaults to the default catalog.
*/
String catalog() default "";
/** (Optional) The schema of the table.
* <p> Defaults to the default schema for user.
*/
String schema() default "";
/**
* (Optional) Unique constraints that are to be placed on
* the table. These are only used if table generation is in
* effect. These constraints apply in addition to any constraints
* specified by the <code>Column</code> and <code>JoinColumn</code>
* annotations and constraints entailed by primary key mappings.
* <p> Defaults to no additional constraints.
*/
UniqueConstraint[] uniqueConstraints() default {};
/**
* (Optional) Indexes for the table. These are only used if
* table generation is in effect. Note that it is not necessary
* to specify an index for a primary key, as the primary key
* index will be created automatically.
*
* @since 2.1
*/
Index[] indexes() default {};
}
์์ ํ ์ด๋ธ์ ์ดํด๋ณด๋ฉด, ํ ์ด๋ธ ์ด๋ฆ๊ณผ ์นดํ๋ก๊ทธ, ์คํค๋ง, ์ธ๋ฑ์ค ๋ฑ์ ๋ํด์ ์ ๊ณต์ ํด์ฃผ๊ณ ์๋ ๊ฒ์ ์ดํด๋ณผ ์ ์๋ค
package com.example.jpa_entity.domain;
import lombok.*;
import javax.persistence.*;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
@RequiredArgsConstructor
@Entity
@Table(name="user")
public class User {
@Id
@GeneratedValue
@Column(name="id")
private Long id;
@NonNull
@Column(name="name")
private String name;
@NonNull
@Column(name="email")
private String email;
@Column(name="created_at")
private Date createdAt;
@Column(name="updated_at")
private Date updatedAt;
//IsNotEmpty ํ์ธ์ฉ
//@OneToMany(fetch=FetchType.EAGER)
//private List<Address> addresses;
@Column(name="active")
private boolean active;
}
๊ทธ๋ฆฌ๊ณ ์์์๋ @Table(name='user')
๋ก ๋ช
์ํด์ฃผ์๋๋ฐ, ํ
์ด๋ธ ์ด๋ฆ์ ์๋์ผ๋ก ํด๋์ค๋ช
๊ณผ ๋งค์นญ๋๊ธฐ ๋๋ฌธ์ @Table
๋ก ๋ช
์ํด์ฃผ์ด๋ ๊ด์ฐฎ๋ค!
๋ค๋ง, ๋ง์ด๊ทธ๋ ์ด์
์ด๋ ์ด๊ด์์
์ ํ๋ ๊ฒฝ์ฐ์๋, @Table(name=ํ
์ด๋ธ๋ช
)
์ฒ๋ผ ๊ธฐ์
ํด์ฃผ์ด์ผ ํ ์ํฉ๋ ์กด์ฌํ๋ค!
- ๋ง์ด๊ทธ๋ ์ด์ : ๋ฐ์ดํฐ๋ ์ํํธ์จ์ด๋ฅผ ๋ค๋ฅธ ์์คํ ์ผ๋ก ์ด๋์ํค๋ ๊ฒ
์ด๋ฒ์๋ ํ ์ด๋ธ ์ด๋ฆ์ ๋ํ๋ด๋ name ์์ฑ์ ๊ฐ์ "user_legacy"๋ก ๋ณ๊ฒฝํด๋ณด์
๋ฌผ๋ก , data.sql์์ ํ ์ด๋ธ๋ช ์ user๋ผ๊ณ ํด๋์๊ธฐ ๋๋ฌธ์, ๋ถ๋ช ํ ์ค๋ฅ๋ ๋ ๊ฒ์ด๋ค
package com.example.jpa_entity.domain;
import lombok.*;
import javax.persistence.*;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
@RequiredArgsConstructor
@Entity
@Table(name="user_legacy")
public class User {
@Id
@GeneratedValue
@Column(name="id")
private Long id;
@NonNull
@Column(name="name")
private String name;
@NonNull
@Column(name="email")
private String email;
@Column(name="created_at")
private Date createdAt;
@Column(name="updated_at")
private Date updatedAt;
//IsNotEmpty ํ์ธ์ฉ
//@OneToMany(fetch=FetchType.EAGER)
//private List<Address> addresses;
@Column(name="active")
private boolean active;
}
create table user_legacy (
id bigint not null,
active boolean,
created_at timestamp,
email varchar(255),
name varchar(255),
updated_at timestamp,
primary key (id)
)
ํ์ง๋ง, ์ฟผ๋ฆฌ์์ ํ ์ด๋ธ์ ์์ฑํ ๋ user_legacy๋ก ์คํ๋์์์ ํ์ธํด๋ณผ ์ ์๋ค
๐ ํ์ง๋ง, ์ผ๋ฐ์ ์ผ๋ก ํ ์ด๋ธ๊ณผ ์ํฐํฐ๋ 1:1 ๊ด๊ณ๊ฐ ๋์ด ์ด๋ฆ์ด ๋์ผํ ๊ฒ์ด ์ข๋ค!
03-1. @Table
์ด๋
ธํ
์ด์
์์ ์ธ๋ฑ์ค์ UNIQUE ์ ์ฝ์กฐ๊ฑด ๋ถ์ฌ์ฃผ๊ธฐ- indexes={@Index(columnList="์ปฌ๋ผ๋ช
")}
, uniqueConstraints={@UniqueConstraint(columnNames={"์ปฌ๋ผ๋ช
"})}
package com.example.jpa_entity.domain;
import lombok.*;
import javax.persistence.*;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
@RequiredArgsConstructor
@Entity
@Table(indexes = {@Index(columnList = "name")},uniqueConstraints = {@UniqueConstraint(columnNames = {"email"})})
public class User {
@Id
@GeneratedValue
@Column(name="id")
private Long id;
@NonNull
@Column(name="name")
private String name;
@NonNull
@Column(name="email")
private String email;
@Column(name="created_at")
private Date createdAt;
@Column(name="updated_at")
private Date updatedAt;
//IsNotEmpty ํ์ธ์ฉ
//@OneToMany(fetch=FetchType.EAGER)
//private List<Address> addresses;
@Column(name="active")
private boolean active;
}
์์์ ์ ๊น ์ธ๊ธํ๋ ๊ฒ์ฒ๋ผ Table ์ด๋ ธํ ์ด์ ๋ด์์๋ ์ธ๋ฑ์ค์ UNIQUE ์ ์ฝ์กฐ๊ฑด์ ๊ฑธ์ด์ค ์ ์๋ค!
์ด๋ ๊ฐ๊ฐ indexes={@Index(columnList="์ปฌ๋ผ๋ช
")}
, uniqueConstraints={@UniqueConstraint(columnNames={"์ปฌ๋ผ๋ช
"})}
์ ์ด์ฉํด์ ์์ ๊ฐ์ด ์ ์ฉ๊ฐ๋ฅํ๋ค!
Hibernate:
drop table if exists address CASCADE
Hibernate:
drop table if exists user CASCADE
Hibernate:
drop sequence if exists hibernate_sequence
Hibernate: create sequence hibernate_sequence start with 1 increment by 1
Hibernate:
create table address (
id bigint not null,
primary key (id)
)
Hibernate:
create table user (
id bigint not null,
active boolean,
created_at timestamp,
email varchar(255),
name varchar(255),
updated_at timestamp,
primary key (id)
)
Hibernate: create index IDXgj2fy3dcix7ph7k8684gka40c on user (name)
Hibernate:
alter table user
add constraint UKob8kqyqqgmefl0aco34akdtpe unique (email)
๊ทธ๋ฆฌ๊ณ ํ ์คํธ๋ฅผ ์คํํด๋ณด๋ฉด DDL์์ `ํ ์ด๋ธ์ด๋ ์ํ์ค๊ฐ ์กด์ฌํ๋ฉด ์ญ์ ํ๋ ๊ฒ์ ๋จผ์ ์ํํ๊ณ ํ ์ด๋ธ์ ๋ง๋๋ ํํ๋ฅผ ํ์ธํด๋ณผ ์ ์๊ณ
์ธ๋ฑ์ค๋ฅผ ๋ง๋ค๊ณ , UNIQUE ์ ์ฝ์กฐ๊ฑด์ ์ถ๊ฐํ๋ ๊ฒ์ ์ดํด๋ณผ ์ ์๋ค!
ํ์ง๋ง ์ด๋ฌํ ์ธ๋ฑ์ค๋ ์ ์ฝ์กฐ๊ฑด์ ๋ํ ์ ์ฉ์ฌํญ์ ์ค์ DB์ ์ ์ฅ๋๋ ๊ฒ๊ณผ๋ ๋ค๋ฅผ ์ ์๋ค! ์ผ๋ฐ์ ์ผ๋ก ๋ง์ด ์ฌ์ฉ๋๋ select, update, insert, delete ์ฟผ๋ฆฌ ์คํ ์์๋ ์ด๋ค ํจ๋ ฅ๋ ์ฃผ์ง ์๋๋ค!
๋ฐ๋ผ์ ์ธ๋ฑ์ค๋ ์ ์ฝ์กฐ๊ฑด์ DB์ ๋งก๊ธฐ๊ณ ์ํฐํฐ์ ํ๊ธฐํ์ง ์๋ ๊ฒฝ์ฐ๊ฐ ๋ณด๋ค ๋ง๋ค!
04. @Column
๊ฐ์ฒด ํ๋์ ๊ฐ ์ปฌ๋ผ ์์ฑ์ ์ง์
- DB๋ ๊ทธ๋๋ก ์ ์งํ๊ณ ์น ์ดํ๋ฆฌ์ผ์ด์ ์ ์๋ก ๋ฆฌ๋น๋ฉํ๋ ๊ฒฝ์ฐ ๋ฑ์ ์ ํฉ
์์ ์๋ ์๋ฅผ ๋ค์ด์ createdAt๊ณผ ๊ฐ์ ํ๋๋ฅผ ๋ชจ์์ ์ต๋ํ ์ ๊ฒ ์ฐ๊ธฐ ์ํด์ crtdat์ ๊ฐ์ ํํ๋ก ์ฌ์ฉํ์๋ค. ํ์ง๋ง ์ด๋ ๊ฐ๋ ์ฑ์ด ๋จ์ด์ง๊ธฐ ๋๋ฌธ์ ์ด ๊ฒฝ์ฐ์ Column์ ์ ์ฉํ๋ฉด ์ข๋ค![A]
package com.example.jpa_entity.domain;
import lombok.*;
import javax.persistence.*;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
@RequiredArgsConstructor
@Entity
@Table(indexes = {@Index(columnList = "name")},uniqueConstraints = {@UniqueConstraint(columnNames = {"email"})})
public class User {
@Id
@GeneratedValue
@Column(name="id")
private Long id;
@NonNull
@Column(name="name")
private String name;
@NonNull
@Column(name="email")
private String email;
@Column(name="created_at")
private Date createdAt;
@Column(name="updated_at")
private Date updatedAt;
//IsNotEmpty ํ์ธ์ฉ
//@OneToMany(fetch=FetchType.EAGER)
//private List<Address> addresses;
@Column(name="active")
private boolean active;
}
๐[A] ๊ณผ๊ฑฐ์ ์ฌ์ฉํ๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค ํ๋๋ช ์ ์๋ฐ ํ๋๋ฅผ ๋งค์นญ์์ผ์ค ๋, ๊ฐ๋ ์ฑ์ UP!
package com.example.jpa_entity.domain;
import lombok.*;
import javax.persistence.*;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
@RequiredArgsConstructor
@Entity
@Table(indexes = {@Index(columnList = "name")},uniqueConstraints = {@UniqueConstraint(columnNames = {"email"})})
public class User {
@Id
@GeneratedValue
@Column(name="id")
private Long id;
@NonNull
@Column(name="name")
private String name;
@NonNull
@Column(name="email")
private String email;
@Column(name="crtdat")
private Date createdAt;
@Column(name="updated_at")
private Date updatedAt;
//IsNotEmpty ํ์ธ์ฉ
//@OneToMany(fetch=FetchType.EAGER)
//private List<Address> addresses;
@Column(name="active")
private boolean active;
}
๊ทธ๋ฌ๋ฉด ์ง๊ธ์ ๋น์ฐํ ์ค๋ฅ๊ฐ ๋ ์๋ฐ์ ์์ง๋ง(data.sql์ insertํ ์ปฌ๋ผ๋ช ์ createdAt์ผ๋ก ์ ์ด๋์๊ธฐ ๋๋ฌธ), DDL๋ง ํ์ธํด๋ณธ๋ค๋ฉด, crtdat์ปฌ๋ผ์ด ์๊ธด ๊ฒ์ ํ์ธํด๋ณผ ์ ์๋ค
Hibernate:
create table user (
id bigint not null,
active boolean,
crtdat timestamp,
email varchar(255),
name varchar(255),
updated_at timestamp,
primary key (id)
)
04-1. @Column-nullable์์ฑ
- ์ผ๋ฐ์ ์ธ ์กฐํ ๊ธฐ๋ฅ ์ฌ์ฉ ์์๋ ,์ฌ์ ์ ๊ฑธ๋ฌ์ฃผ๋ validation ์ญํ ์ ์ํํ์ง ์์
- nullable์ ๊ธฐ๋ณธ์ ์ผ๋ก true๋ก ๋์ด์์
- NOT NULL ์ ์ฝ์กฐ๊ฑด์ ๋ง๋ค์ด์ฃผ๊ธฐ ์ํ ๊ฒ
nullable=false โ๏ธ NOT NULL ์ ์ฝ์กฐ๊ฑด ์ถ๊ฐ
nullable=true โ๏ธ ๋น์ด์์ด๋ ๋๋ค โ๏ธNOT NULL ์ ์ฝ์กฐ๊ฑด ์์
- nullable์ ์ฒดํฌํด์ผํ ์์ฑ์ผ๋ก์จ ์ถํ ํ ์คํธ ๋ฑ์์ ์ ์ฉํ๊ฒ ์ฌ์ฉ๋์ด์ ์์ฃผ ์ฌ์ฉ๋๋ค!
package com.example.jpa_entity.domain;
import lombok.*;
import javax.persistence.*;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
@RequiredArgsConstructor
@Entity
@Table(indexes = {@Index(columnList = "name")},uniqueConstraints = {@UniqueConstraint(columnNames = {"email"})})
public class User {
@Id
@GeneratedValue
@Column(name="id")
private Long id;
@NonNull
@Column(name="name")
private String name;
@NonNull
@Column(name="email")
private String email;
@Column(name="crtdat")
private Date createdAt;
@Column(name="updated_at",nullable = false)
private Date updatedAt;
//IsNotEmpty ํ์ธ์ฉ
//@OneToMany(fetch=FetchType.EAGER)
//private List<Address> addresses;
@Column(name="active")
private boolean active;
}
Hibernate:
create table user (
id bigint not null,
active boolean,
crtdat timestamp,
email varchar(255),
name varchar(255),
updated_at timestamp not null,
primary key (id)
)
๊ทธ๋ฌ๋ฉด, nullable=false๋ฅผ ์ง์ ํด์ค ํ๋์ not null์ด ๋ถ์ ๊ฒ์ ์ ์ ์๋ค
/*
* Copyright (c) 2008, 2019 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
// Contributors:
// Linda DeMichiel - 2.1
// Linda DeMichiel - 2.0
package javax.persistence;
import java.lang.annotation.Target;
import java.lang.annotation.Retention;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* Specifies the mapped column for a persistent property or field.
* If no <code>Column</code> annotation is specified, the default values apply.
*
* <blockquote><pre>
* Example 1:
*
* @Column(name="DESC", nullable=false, length=512)
* public String getDescription() { return description; }
*
* Example 2:
*
* @Column(name="DESC",
* columnDefinition="CLOB NOT NULL",
* table="EMP_DETAIL")
* @Lob
* public String getDescription() { return description; }
*
* Example 3:
*
* @Column(name="ORDER_COST", updatable=false, precision=12, scale=2)
* public BigDecimal getCost() { return cost; }
*
* </pre></blockquote>
*
*
* @since 1.0
*/
@Target({METHOD, FIELD})
@Retention(RUNTIME)
public @interface Column {
/**
* (Optional) The name of the column. Defaults to
* the property or field name.
*/
String name() default "";
/**
* (Optional) Whether the column is a unique key. This is a
* shortcut for the <code>UniqueConstraint</code> annotation at the table
* level and is useful for when the unique key constraint
* corresponds to only a single column. This constraint applies
* in addition to any constraint entailed by primary key mapping and
* to constraints specified at the table level.
*/
boolean unique() default false;
/**
* (Optional) Whether the database column is nullable.
*/
boolean nullable() default true;
/**
* (Optional) Whether the column is included in SQL INSERT
* statements generated by the persistence provider.
*/
boolean insertable() default true;
/**
* (Optional) Whether the column is included in SQL UPDATE
* statements generated by the persistence provider.
*/
boolean updatable() default true;
/**
* (Optional) The SQL fragment that is used when
* generating the DDL for the column.
* <p> Defaults to the generated SQL to create a
* column of the inferred type.
*/
String columnDefinition() default "";
/**
* (Optional) The name of the table that contains the column.
* If absent the column is assumed to be in the primary table.
*/
String table() default "";
/**
* (Optional) The column length. (Applies only if a
* string-valued column is used.)
*/
int length() default 255;
/**
* (Optional) The precision for a decimal (exact numeric)
* column. (Applies only if a decimal column is used.)
* Value must be set by developer if used when generating
* the DDL for the column.
*/
int precision() default 0;
/**
* (Optional) The scale for a decimal (exact numeric) column.
* (Applies only if a decimal column is used.)
*/
int scale() default 0;
}
์๋ Column ์ด๋ ธํ ์ด์ ์ ๋ํ ๋ถ๋ถ์ธ๋ฐ,
- unique๋ unique ์ ์ฝ์กฐ๊ฑด์ ๋ํ ๋ถ๋ถ์ด๋ค
-Table์์๋ ๋ณตํฉ ์ปฌ๋ผ์ ๋ํด unique ์ ์ฝ์กฐ๊ฑด์ ๊ฑธ ์ ์๋ uniqueConstraints๊ฐ ์๋ค๋ฉด,
column์๋ ๋จ๋ ์ ์ผ๋ก ๊ฐ ํ๋์ ๊ฑธ ์ ์๋๋ก ๋์ด์๋ค!
package com.example.jpa_entity.domain;
import lombok.*;
import javax.persistence.*;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
@RequiredArgsConstructor
@Entity
@Table(indexes = {@Index(columnList = "name")},uniqueConstraints = {@UniqueConstraint(columnNames = {"email"})})
public class User {
@Id
@GeneratedValue
@Column(name="id")
private Long id;
@NonNull
@Column(name="name",unique = true)
private String name;
@NonNull
@Column(name="email")
private String email;
@Column(name="crtdat")
private Date createdAt;
@Column(name="updated_at",nullable = false)
private Date updatedAt;
//IsNotEmpty ํ์ธ์ฉ
//@OneToMany(fetch=FetchType.EAGER)
//private List<Address> addresses;
@Column(name="active")
private boolean active;
}
create table user (
id bigint not null,
active boolean,
crtdat timestamp,
email varchar(255),
name varchar(255),
updated_at timestamp not null,
primary key (id)
)
Hibernate: create index IDXgj2fy3dcix7ph7k8684gka40c on user (name)
Hibernate:
alter table user
add constraint UKob8kqyqqgmefl0aco34akdtpe unique (email)
Hibernate:
alter table user
add constraint UK_gj2fy3dcix7ph7k8684gka40c unique (name)
- length()๋ ํด๋น ํ๋์ ๊ธธ์ด๋ฅผ ๋ํ๋ด๋๋ฐ, ๊ธฐ๋ณธ๊ฐ์ด 255์ด๋ผ์ varchar(255)๋ก ํ๊ธฐ๋์ด ์๋ ๊ฒ์ ๋ณผ ์ ์๋ค
์ด๋ฉ์ผ ํ๋์ ๊ธธ์ด๋ฅผ 4000์ผ๋ก ๋๋ ค๋ณด์
package com.example.jpa_entity.domain;
import lombok.*;
import javax.persistence.*;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
@RequiredArgsConstructor
@Entity
@Table(indexes = {@Index(columnList = "name")},uniqueConstraints = {@UniqueConstraint(columnNames = {"email"})})
public class User {
@Id
@GeneratedValue
@Column(name="id")
private Long id;
@NonNull
@Column(name="name",unique = true)
private String name;
@NonNull
@Column(name="email",length = 4000)
private String email;
@Column(name="crtdat")
private Date createdAt;
@Column(name="updated_at",nullable = false)
private Date updatedAt;
//IsNotEmpty ํ์ธ์ฉ
//@OneToMany(fetch=FetchType.EAGER)
//private List<Address> addresses;
@Column(name="active")
private boolean active;
}
create table user (
id bigint not null,
active boolean,
crtdat timestamp,
email varchar(4000),
name varchar(255),
updated_at timestamp not null,
primary key (id)
)
๊ทธ๋ฌ๋ฉด ์ด์ ์ด๋ฉ์ผ ํ๋์ ๊ธธ์ด๋ ๋์ด์ 255๊ฐ ์๋๋ผ 4000์ด ๋์์์ ํ์ธํด๋ณผ ์ ์๋ค
- insertable, updatable: ๋ค๋ฅธ ์์ฑ๋ค๊ณผ ๋ค๋ฅด๊ฒ DDL์ด ์๋ ์ผ๋ฐ์ ์ธ DML ์ฟผ๋ฆฌ์ ์ํฅ์ ๋ผ์นจ
๋จผ์ User ๊ฐ์ฒด๋ฅผ ๋ค์ ์ ๋ฆฌํด๋ณด๊ณ , ์๋์ ๊ฐ์ด ํ ์คํธํด๋ณด์
package com.example.jpa_entity.domain;
import lombok.*;
import javax.persistence.*;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
@RequiredArgsConstructor
@Entity
@Table(indexes = {@Index(columnList = "name")},uniqueConstraints = {@UniqueConstraint(columnNames = {"email"})})
public class User {
@Id
@GeneratedValue
@Column(name="id")
private Long id;
@NonNull
@Column(name="name")
private String name;
@NonNull
@Column(name="email",length = 4000)
private String email;
@Column(name="created_at")
private Date createdAt;
@Column(name="updated_at")
private Date updatedAt;
//IsNotEmpty ํ์ธ์ฉ
//@OneToMany(fetch=FetchType.EAGER)
//private List<Address> addresses;
@Column(name="active")
private boolean active;
}
@Test
public void dmlAccess(){
User user=new User();
user.setName("martin");
user.setEmail("martin2@fastcampus.com");
userRepository.save(user);//insert
User user2=userRepository.findById(1L).orElseThrow(RuntimeException::new);
user2.setEmail("update_1l_id@fastcampus.com");
userRepository.save(user2);//update
}
๊ทธ๋ฌ๋ฉด ๋น์ฐํ, ์ฒ์์๋ insertable๊ณผ updatable์ ๊ธฐ๋ณธ๊ฐ์ด true๋ก ๋์ด์์ด์ insert, update๊ฐ ๊ฐ๋ฅํ์ฌ ์๋์ ๊ฐ์ ์ฟผ๋ฆฌ๋ฅผ ํ์ธํด๋ณผ ์ ์๋ค
Hibernate:
call next value for hibernate_sequence
Hibernate:
insert
into
user
(active, created_at, email, name, updated_at, id)
values
(?, ?, ?, ?, ?, ?)
Hibernate:
select
user0_.id as id1_1_0_,
user0_.active as active2_1_0_,
user0_.created_at as created_3_1_0_,
user0_.email as email4_1_0_,
user0_.name as name5_1_0_,
user0_.updated_at as updated_6_1_0_
from
user user0_
where
user0_.id=?
Hibernate:
select
user0_.id as id1_1_0_,
user0_.active as active2_1_0_,
user0_.created_at as created_3_1_0_,
user0_.email as email4_1_0_,
user0_.name as name5_1_0_,
user0_.updated_at as updated_6_1_0_
from
user user0_
where
user0_.id=?
Hibernate:
update
user
set
active=?,
created_at=?,
email=?,
name=?,
updated_at=?
where
id=?
๐ ์ด๋ฒ์๋ created_at ํ๋์ ๋ํด์ update๋์ง ๋ชปํ๋๋ก updatable=false
๋ฅผ ์ค์ ํด์ฃผ๊ณ , updated_at ํ๋์ ๋ํด์ insert๋์ง ๋ชปํ๋๋ก insertable=false
๋ฅผ ์ค์ ํด์ฃผ์
package com.example.jpa_sort_practice.domain;
import lombok.*;
import javax.persistence.*;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
@RequiredArgsConstructor
@Entity
@Table(name="user")
public class User {
@Id
@GeneratedValue
@Column(name="id")
private Long id;
@NonNull
@Column(name="name")
private String name;
@NonNull
@Column(name="email")
private String email;
@Column(name="created_at",updatable = false)
private Date createdAt;
@Column(name="updated_at",insertable = false)
private Date updatedAt;
//IsNotEmpty ํ์ธ์ฉ
//@OneToMany(fetch=FetchType.EAGER)
//private List<Address> addresses;
@Column(name="active")
private boolean active;
}
๊ทธ๋ฆฌ๊ณ ๊ฒฐ๊ณผ๋ฅผ ๋ ์ฝ๋๋ค์ ๋ฌถ์์ผ๋ก ๋ณด๊ธฐ ์ํด์ ์ถ๋ ฅ์ ํด์ฃผ๋ฉด
@Test
public void dmlAccess(){
User user=new User();
user.setName("martin");
user.setEmail("martin2@fastcampus.com");
userRepository.save(user);//insert
User user2=userRepository.findById(1L).orElseThrow(RuntimeException::new);
user2.setEmail("update_1l_id@fastcampus.com");
userRepository.save(user2);//update
userRepository.findAll().forEach(System.out::println);
}
Hibernate:
call next value for hibernate_sequence
Hibernate:
insert
into
user
(active, created_at, email, name, id)
values
(?, ?, ?, ?, ?)
Hibernate:
select
user0_.id as id1_1_0_,
user0_.active as active2_1_0_,
user0_.created_at as created_3_1_0_,
user0_.email as email4_1_0_,
user0_.name as name5_1_0_,
user0_.updated_at as updated_6_1_0_
from
user user0_
where
user0_.id=?
Hibernate:
select
user0_.id as id1_1_0_,
user0_.active as active2_1_0_,
user0_.created_at as created_3_1_0_,
user0_.email as email4_1_0_,
user0_.name as name5_1_0_,
user0_.updated_at as updated_6_1_0_
from
user user0_
where
user0_.id=?
Hibernate:
update
user
set
active=?,
email=?,
name=?,
updated_at=?
where
id=?
Hibernate:
select
user0_.id as id1_1_,
user0_.active as active2_1_,
user0_.created_at as created_3_1_,
user0_.email as email4_1_,
user0_.name as name5_1_,
user0_.updated_at as updated_6_1_
from
user user0_
User(id=1, name=martin, email=update_1l_id@fastcampus.com, createdAt=2021-08-19 14:32:55.101, updatedAt=2021-08-19 14:32:55.101, active=true)
User(id=2, name=dennis, email=dennis@fastcampus.com, createdAt=2021-08-19 14:32:55.114, updatedAt=2021-08-19 14:32:55.114, active=true)
User(id=3, name=sophia, email=sophia@slowcampus.com, createdAt=2021-08-19 14:32:55.114, updatedAt=2021-08-19 14:32:55.114, active=false)
User(id=4, name=james, email=james@slowcampus.com, createdAt=2021-08-19 14:32:55.114, updatedAt=2021-08-19 14:32:55.114, active=false)
User(id=5, name=martin, email=martin@another.com, createdAt=2021-08-19 14:32:55.115, updatedAt=2021-08-19 14:32:55.115, active=true)
User(id=6, name=martin, email=martin2@fastcampus.com, createdAt=null, updatedAt=null, active=false)
id=6, ์ฆ ์๋ก ์ฝ์ ํ martin์ updated_at ์ด insertable=false์ด๊ธฐ ๋๋ฌธ์ ๊ฐ์์๋, ์ฟผ๋ฆฌ์์๋ ๋น ์ง ๊ฒ์ ํ์ธํด๋ณผ ์ ์๊ณ
id=1์ธ martin์ created_at์ด updatable=false๋ผ์ ์ฟผ๋ฆฌ์์ update ์ created_at์ด ๋น ์ง ๊ฒ์ ํ์ธํด๋ณผ ์ ์๋ค!
๐ ์ํฐํฐ๋ ๋ฐ์ดํฐ์ ๋ํ ๊ฐ์ฒด์ด๊ธฐ ๋๋ฌธ์ DB ๋ ์ฝ๋์ ๊ฐ์ ๊ทธ๋๋ก ๋ฐ์ํ๋ค! ํ์ง๋ง, ๊ฐ์ฒด์ด๊ธฐ ๋๋ฌธ์ ๋ณ๋์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ๊ณ ์ถ์ ๋์ฆ๋ ์๊ธฐ๊ธฐ ๋ง๋ จ์ธ๋ฐ, ์ด๋ฅผ ์ํด ์กด์ฌํ๋ ์ด๋
ธํ
์ด์
์ด ๋ฐ๋ก @Transient
์ด๋ค!
05. @Transient
์ถ๊ฐ์ ์ผ๋ก, testData๋ผ๋ ํ๋๋ฅผ User ๊ฐ์ฒด์ ์ถ๊ฐํด๋ณด์
package com.example.jpa_entity.domain;
import lombok.*;
import javax.persistence.*;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
@RequiredArgsConstructor
@Entity
@Table(name="user")
public class User {
@Id
@GeneratedValue
@Column(name="id")
private Long id;
@NonNull
@Column(name="name")
private String name;
@NonNull
@Column(name="email")
private String email;
@Column(name="created_at",updatable = false)
private Date createdAt;
@Column(name="updated_at",insertable = false)
private Date updatedAt;
//IsNotEmpty ํ์ธ์ฉ
//@OneToMany(fetch=FetchType.EAGER)
//private List<Address> addresses;
@Column(name="active")
private boolean active;
private String testData;
}
Hibernate:
create table user (
id bigint not null,
active boolean,
created_at timestamp,
email varchar(255),
name varchar(255),
test_data varchar(255),
updated_at timestamp,
primary key (id)
)
(์ค๋ต)
Hibernate:
call next value for hibernate_sequence
Hibernate:
insert
into
user
(active, created_at, email, name, test_data, id)
values
(?, ?, ?, ?, ?, ?)
Hibernate:
select
user0_.id as id1_1_0_,
user0_.active as active2_1_0_,
user0_.created_at as created_3_1_0_,
user0_.email as email4_1_0_,
user0_.name as name5_1_0_,
user0_.test_data as test_dat6_1_0_,
user0_.updated_at as updated_7_1_0_
from
user user0_
where
user0_.id=?
Hibernate:
select
user0_.id as id1_1_0_,
user0_.active as active2_1_0_,
user0_.created_at as created_3_1_0_,
user0_.email as email4_1_0_,
user0_.name as name5_1_0_,
user0_.test_data as test_dat6_1_0_,
user0_.updated_at as updated_7_1_0_
from
user user0_
where
user0_.id=?
Hibernate:
update
user
set
active=?,
email=?,
name=?,
test_data=?,
updated_at=?
where
id=?
Hibernate:
select
user0_.id as id1_1_,
user0_.active as active2_1_,
user0_.created_at as created_3_1_,
user0_.email as email4_1_,
user0_.name as name5_1_,
user0_.test_data as test_dat6_1_,
user0_.updated_at as updated_7_1_
from
user user0_
User(id=1, name=martin, email=update_1l_id@fastcampus.com, createdAt=2021-08-19 14:43:22.501, updatedAt=2021-08-19 14:43:22.501, active=true, testData=null)
User(id=2, name=dennis, email=dennis@fastcampus.com, createdAt=2021-08-19 14:43:22.517, updatedAt=2021-08-19 14:43:22.517, active=true, testData=null)
User(id=3, name=sophia, email=sophia@slowcampus.com, createdAt=2021-08-19 14:43:22.518, updatedAt=2021-08-19 14:43:22.518, active=false, testData=null)
User(id=4, name=james, email=james@slowcampus.com, createdAt=2021-08-19 14:43:22.518, updatedAt=2021-08-19 14:43:22.518, active=false, testData=null)
User(id=5, name=martin, email=martin@another.com, createdAt=2021-08-19 14:43:22.519, updatedAt=2021-08-19 14:43:22.519, active=true, testData=null)
User(id=6, name=martin, email=martin2@fastcampus.com, createdAt=null, updatedAt=null, active=false, testData=null)
๊ทธ๋ฌ๋ฉด, test_data๋ผ๋ ํ๋๊ฐ ์๊ฒจ๋๊ณ , insert์ update ์ ํด๋น ํ๋๊ฐ ๋ ธ์ถ๋๋ ๊ฒ์ ํ์ธํด๋ณผ ์ ์๋ค
ํ์ง๋ง ์ด testData๋ DB์๋ ๋ฐ์ํ์ง ์๊ณ ๊ฐ์ฒด์์๋ง ์ฌ์ฉํ๊ณ ์ถ์ ์ค๋ธ์ ํธ์ ์์ฑ์ผ ์๋ ์๋ค
๐์ด๋ @Transient
๋ฅผ ๋ถ์ฌ์ฃผ๋ฉด, ์ด ์ด๋
ธํ
์ด์
์ด ๋ถ์ ํ๋๋ DB์ ๋ฐ์๋์ง ์์์ ๋ํ๋ผ ์ ์๋ค
package com.example.jpa_entity.domain;
import lombok.*;
import javax.persistence.*;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
@RequiredArgsConstructor
@Entity
@Table(name="user")
public class User {
@Id
@GeneratedValue
@Column(name="id")
private Long id;
@NonNull
@Column(name="name")
private String name;
@NonNull
@Column(name="email")
private String email;
@Column(name="created_at",updatable = false)
private Date createdAt;
@Column(name="updated_at",insertable = false)
private Date updatedAt;
//IsNotEmpty ํ์ธ์ฉ
//@OneToMany(fetch=FetchType.EAGER)
//private List<Address> addresses;
@Column(name="active")
private boolean active;
@Transient
private String testData;
}
create table user (
id bigint not null,
active boolean,
created_at timestamp,
email varchar(255),
name varchar(255),
updated_at timestamp,
primary key (id)
)
Hibernate:
call next value for hibernate_sequence
Hibernate:
insert
into
user
(active, created_at, email, name, id)
values
(?, ?, ?, ?, ?)
Hibernate:
select
user0_.id as id1_1_0_,
user0_.active as active2_1_0_,
user0_.created_at as created_3_1_0_,
user0_.email as email4_1_0_,
user0_.name as name5_1_0_,
user0_.updated_at as updated_6_1_0_
from
user user0_
where
user0_.id=?
Hibernate:
select
user0_.id as id1_1_0_,
user0_.active as active2_1_0_,
user0_.created_at as created_3_1_0_,
user0_.email as email4_1_0_,
user0_.name as name5_1_0_,
user0_.updated_at as updated_6_1_0_
from
user user0_
where
user0_.id=?
Hibernate:
update
user
set
active=?,
email=?,
name=?,
updated_at=?
where
id=?
Hibernate:
select
user0_.id as id1_1_,
user0_.active as active2_1_,
user0_.created_at as created_3_1_,
user0_.email as email4_1_,
user0_.name as name5_1_,
user0_.updated_at as updated_6_1_
from
user user0_
๊ทธ๋ฌ๋ฉด ์์ ํ์ธ๋์๋ testData๊ฐ DB ํ๋์๋ ์ ํ ๋ฐ์์ด ๋์ง ์๋ ๊ฒ(DDL, DML์ ๋ชจ๋ ๋ฐ์x
โถ๏ธ ์์์ฑx)์ ํ์ธํด๋ณผ ์ ์๋ค
๊ทธ๋ฆฌ๊ณ ์ด๋ฌ๋ฉด์ ํด๋น ์ํฐํฐ์ ์๋ช
์ฃผ๊ธฐ๋ฅผ ๊ฐ์ดํ๊ฒ ๋๋ค
๐ 06. Enum์ ๋ํ ์ฒ๋ฆฌ
๋จผ์ ์๋์ ๊ฐ์ด ์ฑ๋ณ์ ๋ํ enum์ ๋ง๋ค์ด๋ณด์
package com.example.jpa_entity.domain;
public enum Gender {
MALE,
FEMALE
}
๊ทธ๋ฆฌ๊ณ ์ด๋ฌํ enum์ User์ ํ๋๋ก์จ ๋ฃ์ด์ฃผ๋๋ก ํ์
package com.example.jpa_entity.domain;
import lombok.*;
import javax.persistence.*;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
@RequiredArgsConstructor
@Entity
@Table(name="user")
public class User {
@Id
@GeneratedValue
@Column(name="id")
private Long id;
@NonNull
@Column(name="name")
private String name;
@NonNull
@Column(name="email")
private String email;
@Column(name="created_at",updatable = false)
private Date createdAt;
@Column(name="updated_at",insertable = false)
private Date updatedAt;
//IsNotEmpty ํ์ธ์ฉ
//@OneToMany(fetch=FetchType.EAGER)
//private List<Address> addresses;
@Column(name="active")
private boolean active;
@Transient
private String testData;
//enum
private Gender gender;
}
๊ทธ๋ฆฌ๊ณ ์์ด๋๊ฐ 1์ธ user๋ฅผ ์ฐพ์์ ์ฑ๋ณ์ MALE๋ก ์ค์ ํ๋ update๋ฅผ ์คํํด์ฃผ์
@Test
public void enumTest(){
User user=userRepository.findById(1L).orElseThrow(RuntimeException::new);
user.setGender(Gender.MALE);
userRepository.save(user);//update
userRepository.findAll().forEach(System.out::println);
}
๊ทธ๋ฌ๋ฉด ์๋์ฒ๋ผ ์์ด๋๊ฐ 1์ธ ์ฌ์ฉ์๋ง gender๊ฐ์ผ๋ก MALE์ด ๋ค์ด๊ฐ ๊ฒ์ ํ์ธํด๋ณผ ์ ์๋ค
Hibernate:
select
user0_.id as id1_1_0_,
user0_.active as active2_1_0_,
user0_.created_at as created_3_1_0_,
user0_.email as email4_1_0_,
user0_.gender as gender5_1_0_,
user0_.name as name6_1_0_,
user0_.updated_at as updated_7_1_0_
from
user user0_
where
user0_.id=?
Hibernate:
select
user0_.id as id1_1_0_,
user0_.active as active2_1_0_,
user0_.created_at as created_3_1_0_,
user0_.email as email4_1_0_,
user0_.gender as gender5_1_0_,
user0_.name as name6_1_0_,
user0_.updated_at as updated_7_1_0_
from
user user0_
where
user0_.id=?
Hibernate:
update
user
set
active=?,
email=?,
gender=?,
name=?,
updated_at=?
where
id=?
Hibernate:
select
user0_.id as id1_1_,
user0_.active as active2_1_,
user0_.created_at as created_3_1_,
user0_.email as email4_1_,
user0_.gender as gender5_1_,
user0_.name as name6_1_,
user0_.updated_at as updated_7_1_
from
user user0_
User(id=1, name=martin, email=martin@fastcampus.com, createdAt=2021-08-20 14:04:02.216, updatedAt=2021-08-20 14:04:02.216, active=true, testData=null, gender=MALE)
User(id=2, name=dennis, email=dennis@fastcampus.com, createdAt=2021-08-20 14:04:02.232, updatedAt=2021-08-20 14:04:02.232, active=true, testData=null, gender=null)
User(id=3, name=sophia, email=sophia@slowcampus.com, createdAt=2021-08-20 14:04:02.232, updatedAt=2021-08-20 14:04:02.232, active=false, testData=null, gender=null)
User(id=4, name=james, email=james@slowcampus.com, createdAt=2021-08-20 14:04:02.233, updatedAt=2021-08-20 14:04:02.233, active=false, testData=null, gender=null)
User(id=5, name=martin, email=martin@another.com, createdAt=2021-08-20 14:04:02.233, updatedAt=2021-08-20 14:04:02.233, active=true, testData=null, gender=null)
enum์ ์ฌ์ฉํ ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฃจ๋ ๊ฒ์ ๋ณด๋ค ์ดํดํ๊ธฐ ์ํค์ ์์ง ๋ฐฐ์ฐ์ง๋ ์์์ง๋ง ๋ค์ดํฐ๋ธ ์ฟผ๋ฆฌ๋ฅผ ์ด์ฉํด๋ณด์
package com.example.jpa_entity.repository;
import com.example.jpa_entity.domain.User;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
import java.util.Map;
public interface UserRepository extends JpaRepository<User,Long> {
List<User> findTop1ByName(String name);
List<User> findTop1ByNameOrderByIdDesc(String name);
List<User> findFirstByNameOrderByIdDescEmailAsc(String name);
List<User> findFirstByName(String name, Sort sort);
@Query(value="select * from user limit 1;", nativeQuery = true)
Map<String, Object> findRawRecord();
}
๊ทธ๋ฆฌ๊ณ ๊ฐ๋จํ๊ฒ ํ ์คํธํด๋ณด์
@Test
public void enumTest(){
User user=userRepository.findById(1L).orElseThrow(RuntimeException::new);
user.setGender(Gender.MALE);
userRepository.save(user);//update
userRepository.findAll().forEach(System.out::println);
System.out.println(userRepository.findRawRecord().get("gender"));
}
Hibernate:
select
user0_.id as id1_1_0_,
user0_.active as active2_1_0_,
user0_.created_at as created_3_1_0_,
user0_.email as email4_1_0_,
user0_.gender as gender5_1_0_,
user0_.name as name6_1_0_,
user0_.updated_at as updated_7_1_0_
from
user user0_
where
user0_.id=?
Hibernate:
select
user0_.id as id1_1_0_,
user0_.active as active2_1_0_,
user0_.created_at as created_3_1_0_,
user0_.email as email4_1_0_,
user0_.gender as gender5_1_0_,
user0_.name as name6_1_0_,
user0_.updated_at as updated_7_1_0_
from
user user0_
where
user0_.id=?
Hibernate:
update
user
set
active=?,
email=?,
gender=?,
name=?,
updated_at=?
where
id=?
Hibernate:
select
user0_.id as id1_1_,
user0_.active as active2_1_,
user0_.created_at as created_3_1_,
user0_.email as email4_1_,
user0_.gender as gender5_1_,
user0_.name as name6_1_,
user0_.updated_at as updated_7_1_
from
user user0_
User(id=1, name=martin, email=martin@fastcampus.com, createdAt=2021-08-20 14:09:55.478, updatedAt=2021-08-20 14:09:55.478, active=true, testData=null, gender=MALE)
User(id=2, name=dennis, email=dennis@fastcampus.com, createdAt=2021-08-20 14:09:55.494, updatedAt=2021-08-20 14:09:55.494, active=true, testData=null, gender=null)
User(id=3, name=sophia, email=sophia@slowcampus.com, createdAt=2021-08-20 14:09:55.494, updatedAt=2021-08-20 14:09:55.494, active=false, testData=null, gender=null)
User(id=4, name=james, email=james@slowcampus.com, createdAt=2021-08-20 14:09:55.495, updatedAt=2021-08-20 14:09:55.495, active=false, testData=null, gender=null)
User(id=5, name=martin, email=martin@another.com, createdAt=2021-08-20 14:09:55.495, updatedAt=2021-08-20 14:09:55.495, active=true, testData=null, gender=null)
Hibernate:
select
*
from
user limit 1;
0
๊ทธ๋ฌ๋ฉด @Query
์์ ๋ด์ฉ์ผ๋ก ๋ณด์ USER ํ
์ด๋ธ์์ 1๊ฐ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๋ ๋ด์ฉ์ผ๋ก ์ถ์ธก๋๋ค
๊ทธ๋ฆฌ๊ณ get("gender")๋ฅผ ํ์ ๋์ ๋ํ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ๋ผ๋ณด๋ ๊ฒ์ด ๋ฐ๋ก ์์ ํ ์คํธ์ด๋ค
ํ์ง๋ง 0์ด ์ถ๋ ฅ๋๋ ๊ฒ์ ํ์ธํด๋ณผ ์ ์๋ค!
ํ์ง๋ง enum์ ๋ด์ฉ์ ์๋์ฒ๋ผ ๋ณ๊ฒฝํ๋ฉด 1์ด ์ถ๋ ฅ๋๋ ๊ฒ์ ํ์ธํด๋ณผ ์ ์๋ค.
package com.example.jpa_entity.domain;
public enum Gender {
FEMALE,
MALE
}
Hibernate:
select
user0_.id as id1_1_0_,
user0_.active as active2_1_0_,
user0_.created_at as created_3_1_0_,
user0_.email as email4_1_0_,
user0_.gender as gender5_1_0_,
user0_.name as name6_1_0_,
user0_.updated_at as updated_7_1_0_
from
user user0_
where
user0_.id=?
Hibernate:
select
user0_.id as id1_1_0_,
user0_.active as active2_1_0_,
user0_.created_at as created_3_1_0_,
user0_.email as email4_1_0_,
user0_.gender as gender5_1_0_,
user0_.name as name6_1_0_,
user0_.updated_at as updated_7_1_0_
from
user user0_
where
user0_.id=?
Hibernate:
update
user
set
active=?,
email=?,
gender=?,
name=?,
updated_at=?
where
id=?
Hibernate:
select
user0_.id as id1_1_,
user0_.active as active2_1_,
user0_.created_at as created_3_1_,
user0_.email as email4_1_,
user0_.gender as gender5_1_,
user0_.name as name6_1_,
user0_.updated_at as updated_7_1_
from
user user0_
User(id=1, name=martin, email=martin@fastcampus.com, createdAt=2021-08-20 14:15:04.59, updatedAt=2021-08-20 14:15:04.59, active=true, testData=null, gender=MALE)
User(id=2, name=dennis, email=dennis@fastcampus.com, createdAt=2021-08-20 14:15:04.602, updatedAt=2021-08-20 14:15:04.602, active=true, testData=null, gender=null)
User(id=3, name=sophia, email=sophia@slowcampus.com, createdAt=2021-08-20 14:15:04.603, updatedAt=2021-08-20 14:15:04.603, active=false, testData=null, gender=null)
User(id=4, name=james, email=james@slowcampus.com, createdAt=2021-08-20 14:15:04.603, updatedAt=2021-08-20 14:15:04.603, active=false, testData=null, gender=null)
User(id=5, name=martin, email=martin@another.com, createdAt=2021-08-20 14:15:04.604, updatedAt=2021-08-20 14:15:04.604, active=true, testData=null, gender=null)
Hibernate:
select
*
from
user limit 1;
1
User์ Gender ํ๋์ @Enumerated
๋ฅผ ๋ถ์ฌ๋ณด์
package com.example.jpa_entity.domain;
import lombok.*;
import javax.persistence.*;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
@RequiredArgsConstructor
@Entity
@Table(name="user")
public class User {
@Id
@GeneratedValue
@Column(name="id")
private Long id;
@NonNull
@Column(name="name")
private String name;
@NonNull
@Column(name="email")
private String email;
@Column(name="created_at",updatable = false)
private Date createdAt;
@Column(name="updated_at",insertable = false)
private Date updatedAt;
//IsNotEmpty ํ์ธ์ฉ
//@OneToMany(fetch=FetchType.EAGER)
//private List<Address> addresses;
@Column(name="active")
private boolean active;
@Transient
private String testData;
//enum
@Enumerated
private Gender gender;
}
๊ทธ๋ฆฌ๊ณ Enumerated๋ฅผ ์ ๋ณด๋ฉด Enum์ ํ์ ์ ๋ํ ๊ธฐ๋ณธ๊ฐ์ด ORDINAL ์ฆ ์์๋ก ๋์ด ์๋ ๊ฒ์ ํ์ธํด๋ณผ ์ ์๋ค
(์ด์ธ์๋ String์ด ๊ฐ๋ฅ)
๐ ์ฆ, ์ฒซ๋ฒ์งธ์ ์์นํ ๊ฐ์ด 0์ด๊ณ , ๊ทธ ๋ค์์ด 1 ์ด๋ฐ ์์๋ก ๋งค๊ฒจ์ ธ์ DB์ ์ ์ฅ๋๊ธฐ ๋๋ฌธ์ ๊ทธ๋ฐ๊ฒ์ด์๋ค! ์ด๋ ํ ์คํธ๋ก๋ ํ์ธํ๊ธฐ ์ด๋ ต๋ค(MALE, FEMALE๋ก ์ถ๋ ฅ๋๊ธฐ ๋๋ฌธ)
ํ์ง๋ง ์ง๊ธ ํ ์คํธํด๋ณธ ๊ฒ์ฒ๋ผ ์ ์ฅ ์์๊ฐ ๋ฐ๋๊ฑฐ๋, ํน์ ์ด์ธ์ ์ํฉ์ผ๋ก ๊ฐ์ ์ถ๊ฐํ๋ฉด ์ ์ฌ์ ์ธ ๋ฒ๊ทธ๊ฐ ์ผ์ด๋ ์ ์๊ธฐ ๋๋ฌธ์ ORDINAL๋ก ๋ค๋ฃจ๋ ๊ฒ์ ๋ฐ๋์งํ์ง ๋ชปํ๋ค
๐ ๋ฐ๋ผ์ ๋ฐ๋์ Enum์ ํ๋๋ก ์ฌ์ฉํ๋ ค๋ฉด @Enumerated(value=EnumType.String)
์ ์ค์ ํ๊ณ ์ฌ์ฉํด์ฃผ์!!
package com.example.jpa_entity.domain;
import lombok.*;
import javax.persistence.*;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
@RequiredArgsConstructor
@Entity
@Table(name="user")
public class User {
@Id
@GeneratedValue
@Column(name="id")
private Long id;
@NonNull
@Column(name="name")
private String name;
@NonNull
@Column(name="email")
private String email;
@Column(name="created_at",updatable = false)
private Date createdAt;
@Column(name="updated_at",insertable = false)
private Date updatedAt;
//IsNotEmpty ํ์ธ์ฉ
//@OneToMany(fetch=FetchType.EAGER)
//private List<Address> addresses;
@Column(name="active")
private boolean active;
@Transient
private String testData;
//enum
@Enumerated(EnumType.STRING)
private Gender gender;
}
Hibernate:
select
user0_.id as id1_1_0_,
user0_.active as active2_1_0_,
user0_.created_at as created_3_1_0_,
user0_.email as email4_1_0_,
user0_.gender as gender5_1_0_,
user0_.name as name6_1_0_,
user0_.updated_at as updated_7_1_0_
from
user user0_
where
user0_.id=?
Hibernate:
select
user0_.id as id1_1_0_,
user0_.active as active2_1_0_,
user0_.created_at as created_3_1_0_,
user0_.email as email4_1_0_,
user0_.gender as gender5_1_0_,
user0_.name as name6_1_0_,
user0_.updated_at as updated_7_1_0_
from
user user0_
where
user0_.id=?
Hibernate:
update
user
set
active=?,
email=?,
gender=?,
name=?,
updated_at=?
where
id=?
Hibernate:
select
user0_.id as id1_1_,
user0_.active as active2_1_,
user0_.created_at as created_3_1_,
user0_.email as email4_1_,
user0_.gender as gender5_1_,
user0_.name as name6_1_,
user0_.updated_at as updated_7_1_
from
user user0_
User(id=1, name=martin, email=martin@fastcampus.com, createdAt=2021-08-20 14:21:14.97, updatedAt=2021-08-20 14:21:14.97, active=true, testData=null, gender=MALE)
User(id=2, name=dennis, email=dennis@fastcampus.com, createdAt=2021-08-20 14:21:14.981, updatedAt=2021-08-20 14:21:14.981, active=true, testData=null, gender=null)
User(id=3, name=sophia, email=sophia@slowcampus.com, createdAt=2021-08-20 14:21:14.982, updatedAt=2021-08-20 14:21:14.982, active=false, testData=null, gender=null)
User(id=4, name=james, email=james@slowcampus.com, createdAt=2021-08-20 14:21:14.983, updatedAt=2021-08-20 14:21:14.983, active=false, testData=null, gender=null)
User(id=5, name=martin, email=martin@another.com, createdAt=2021-08-20 14:21:14.983, updatedAt=2021-08-20 14:21:14.983, active=true, testData=null, gender=null)
Hibernate:
select
*
from
user limit 1;
๐MALE๐
๊ทธ๋ฌ๋ฉด ๋์ด์ 0์ด๋ 1๋ก ์ ์ฅ ์์์ ๋ฐ๋ผ ๋ฐ๋์ง ์๊ณ , MALE๋ก DB์ ์ ์ฅ๋์์์ ํ์ธํด๋ณผ ์ ์๋ค!
๐ ์ถํ DB ์ฅ์ ๋ ๋ง์ด๊ทธ๋ ์ด์ ์ ๋ฌธ์ ๋ฅผ ๋ฐฉ์งํ๊ธฐ ์ํด์ Enum ์ฌ์ฉ์ ์ฃผ์ํ์!